import Vue from "vue";
import Transaction from "./index.vue";
/**
 * 待支付类方法
 * @param {object} params 订单查询参数
 * @param {object} goodsInfo 商品有关
 * @param {object} jumpDataCustom 支付跳转数据
 * @param {object} orderInfo 订单有关
 * @param {boolean} isNeedHB 是否需要花呗分期
 * @param {boolean} isNeedVirtualPay 是否需要虚拟支付
 */
export default class RePlayBuy {
  constructor(goodsInfo = {}, jumpDataCustom = {}, orderInfo = {}, isNeedHB = false, isNeedVirtualPay = false) {
    this.goodsInfo = goodsInfo;
    this.jumpDataCustom = jumpDataCustom;
    this.orderInfo = orderInfo;
    this.isNeedHB = isNeedHB;
    this.isNeedVirtualPay = isNeedVirtualPay;
  }

  /**
   * 获取支付方式
   * @returns 返回一个对象
   * {  way ,... }payWayInfo: 支付方式对象  花呗分期场景
   * Number 支付方式
   */
  getPayWay() {
    let promiseResolve
    const resultPromise = new Promise((resolve) => {
      promiseResolve = resolve;
    })
    const TransactionConstructor = Vue.extend(Transaction);
    const instance = new TransactionConstructor({
      propsData: {
        goodsInfo: this.goodsInfo,
        jumpDataCustom: this.jumpDataCustom,
        orderInfo: this.orderInfo,
        isNeedHB: this.isNeedHB,
        isNeedVirtualPay: this.isNeedVirtualPay,
        promiseResolve
      },
    });
    instance.$mount();
    instance.initData();
    document.body.appendChild(instance.$el);

    return resultPromise
  }

  /**
   * 下单
   * @param {*} data payWayInfo 支付方式对象
   */
  perform(data = {}) {
    const TransactionConstructor = Vue.extend(Transaction);
    const instance = new TransactionConstructor({
      propsData: {
        goodsInfo: this.goodsInfo,
        jumpDataCustom: this.jumpDataCustom,
        orderInfo: this.orderInfo,
        isNeedHB: this.isNeedHB,
      },
    });
    instance.$mount();
    instance.buy(data);
    document.body.appendChild(instance.$el);
  }
}
